package com.luyuan.crm.sale.web.converter;

import javax.annotation.Resource;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.Customer;
import com.luyuan.crm.member.service.CustomerService;
import com.ruijie.util.StringUtils;

@Component("accountMemberConverter")
public class AccountMemberConverter implements Converter {
    @Resource(name = "customerService")
    private CustomerService customerService;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (StringUtils.isNotEmpty(value)) {
            return customerService.getById(value);
        }

        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value instanceof Customer) {
            return ((Customer) value).getId();
        }

        return null;
    }

}
